/*********************************************************************************
 * Software License Agreement (BSD License)
 * Copyright (c) 2012, Carlos D. Correa (info@ngraph.org)
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of netzen nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************************/
#include "qShortcutBar.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTabWidget>
#include <QToolBar>
#include <QActionGroup>

#include <assert.h>

#include "controller.h"

class ShortcutButton:public QPushButton {
public:
	string fullname;
	ShortcutButton(string fullname): QPushButton() {
		this->fullname = fullname;
		int pos = fullname.find("/");
		string suffix = fullname.substr(pos+1);
		this->setText(suffix.c_str());
	}
};

QShortcutBar::QShortcutBar(Controller *controller, QWidget *parent): QWidget(parent) {
//	this->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
//	this->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

    QHBoxLayout *boxLayout = new QHBoxLayout(this);
	QTabWidget *tab = new QTabWidget(this);
	
	assert(controller);
	this->controller = controller;
	vector<string> shortcuts = controller->getShortcutNames();
	
	set<string> classes;
	map<string, vector<string> > shortcutsByClass;
	for(unsigned int i=0;i<shortcuts.size();i++) {
		int pos = shortcuts[i].find("/");
		string classname = shortcuts[i].substr(0, pos);
		classes.insert(classname);
		shortcutsByClass[classname].push_back(shortcuts[i]);
	}
	
//	tab->setTabShape(QTabWidget::Triangular);
	tab->setFont(QFont("Verdana", 10));
	tab->setTabPosition(QTabWidget::North);
	for(set<string>::iterator it = classes.begin(); it!=classes.end(); ++it ){
		string name = *it;
		//QWidget *widget = new QWidget(this);
		//QHBoxLayout *boxLayout = new QHBoxLayout(widget);
		//widget->setStyleSheet("* { background-color: rgb(255,255,255); padding:0px}");
		QToolBar *toolbar = new QToolBar(name.c_str());		
		//QActionGroup* actionGroup = new QActionGroup(this);
		for(unsigned int i=0;i<shortcutsByClass[name].size();i++) {
			/*
			ShortcutButton *button = new ShortcutButton(shortcutsByClass[name][i].c_str());
			button->setFont(QFont("Arial", 10));
			//button->setMaximumWidth(100);
			button->setMinimumWidth(50);
			button->setStyleSheet("* { background-color: rgb(242,245,245); height:20px; padding:0px;border-style:inset; border-width:1px; border-radius:5px; }");
			connect(button, SIGNAL(clicked()), SLOT(shortcutClicked()));
			boxLayout->addWidget(button);
			 */
			string fullname = shortcutsByClass[name][i];
			int pos = fullname.find("/");
			string suffix = fullname.substr(pos+1);
			QAction *action = toolbar->addAction(suffix.c_str());
			action->setObjectName(fullname.c_str());
			toolbar->addSeparator();
			//ShortcutButton *but1 = new ShortcutButton(shortcutsByClass[name][i].c_str());
			//connect(but1, SIGNAL(clicked()), SLOT(shortcutClicked()));
			//but1->setFont(QFont("Verdana", 10));
			//but1->setMinimumHeight(20);
			//but1->setStyleSheet("* {background-color: rgba(200,200,200,0); border:0px}");
			//toolbar->insertWidget(0, but1);
		}
		connect(toolbar, SIGNAL(actionTriggered(QAction*)), this, SLOT(actionClicked(QAction*)));
		
		
		//widget->setLayout(boxLayout);
		tab->addTab(toolbar, name.c_str());
	}
	
	/*
	for(unsigned int i=0;i<shortcuts.size();i++) {
		QPushButton *button = new QPushButton(shortcuts[i].c_str());
		button->setFont(QFont("Verdana", 10));
		connect(button, SIGNAL(clicked()), SLOT(shortcutClicked()));
		boxLayout->addWidget(button);
	}
	 */
	//setWidget(tab);
	boxLayout->addWidget(tab);
    setLayout(boxLayout);
}

void QShortcutBar::actionClicked(QAction *act) {
	string name = act->objectName().toStdString();
	Shortcut * shortcut = controller->getShortcut(name);
	assert(shortcut);
	shortcut->run();
}

void QShortcutBar::shortcutClicked() {
	ShortcutButton *button = (ShortcutButton *)sender();
	string name = button->fullname;
	Shortcut * shortcut = controller->getShortcut(name);
	assert(shortcut);
	shortcut->run();
	
}